package com.iwdnb.gkgz.application.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.iwdnb.gkgz.application.service.StockChartService;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StrategyTradeDTO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STRATEGY_TRADE;

@Service
public class StockChartServiceImpl implements StockChartService {
    @Autowired
    private StockDayDataRepository stockDayDataRepository;
    @Autowired
    private MetaDataDAO metaDataDAO;

    @Override
    public List<StockDayData> getStockKlineData(String code, String date, Integer period) {
        if (Objects.isNull(period) || period == 0) {
            period = 60;
        }
        period = period * 2;
        Date d = DateUtil.parseDate(date);
        String begin = DateUtil.formatDate(DateUtil.offsetDay(d, 0 - period));
        String end = DateUtil.formatDate(DateUtil.offsetDay(d, period));
        List<StockDayData> stockDayDataList = stockDayDataRepository.queryAllByDateRange(code, begin, end);
        List<String> dateList= Lists.newArrayList(date.split(","));
        for (StockDayData stockDayData : stockDayDataList) {
            if (dateList.contains(stockDayData.getDate())) {
                stockDayData.setMark("买");
            }
        }
        return stockDayDataList;
    }

    @Override
    public List<StockDayData> getStockKlineData(String code, String uuid) {
        Date buyDate = DateUtil.parseDate("2010-01-01");
        Date sellDate = new Date();
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("uuid", uuid);
        metaQuery.addParam("status", "sell");
        List<StrategyTradeDTO> strategyTradeDTOList = metaDataDAO.queryListByCondition(OBJECT_CODE_STRATEGY_TRADE,
            metaQuery,
            StrategyTradeDTO.class);
        List<String> buyDates = new ArrayList<>();
        List<String> sellDates = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(strategyTradeDTOList)) {
            buyDate = strategyTradeDTOList.get(0).getBuyDate();
            sellDate = strategyTradeDTOList.get(strategyTradeDTOList.size() - 1).getSellDate();
            for (StrategyTradeDTO strategyTradeDTO : strategyTradeDTOList) {
                if (StringUtils.equals(strategyTradeDTO.getStatus(), "sell")) {
                    buyDates.add(DateUtil.formatDate(strategyTradeDTO.getBuyDate()));
                    sellDates.add(DateUtil.formatDate(strategyTradeDTO.getSellDate()));
                }
            }
        }
        String begin = DateUtil.formatDate(DateUtil.offsetDay(buyDate, -365));
        String end = DateUtil.formatDate(DateUtil.offsetDay(sellDate, 365));
        List<StockDayData> stockDayDataList = stockDayDataRepository.queryAllByDateRange(code,
            begin, end);
        for (StockDayData stockDayData : stockDayDataList) {
            String date = stockDayData.getDate();
            String mark = "";
            if (sellDates.contains(date)) {
                mark += "卖";
                stockDayData.setMark(mark);
            }
            if (buyDates.contains(date)) {
                mark = StringUtils.isBlank(mark) ? "买" : mark + "/买";
                stockDayData.setMark(mark);
            }
        }
        return stockDayDataList;
    }
}
